package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.list.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 李旭
 * @date 2021/11/24 16:16
 * @Description:
 *
 *   搜索页面
 *
 *   'http://list.gmall.com/list.html?keyword=' + this.keyword
 */
@Controller
public class ListController {

    @Autowired
    private ListFeignClient listFeignClient;

    //开始搜索
    @GetMapping("/list.html")
    public String list(SearchParam searchParam, Model model){
        //远程调用 搜索微服务的ES索引库中的数据
        SearchResponseVo vo = listFeignClient.search(searchParam);
        List<Goods> goodsList = vo.getGoodsList();
        List<SearchResponseTmVo> trademarkList = vo.getTrademarkList();
        List<SearchResponseAttrVo> attrsList = vo.getAttrsList();
        Long totalPages = vo.getTotalPages();
        //页面渲染
        //1:回显关键词
        model.addAttribute("searchParam",searchParam);
        //2：品牌集合
        model.addAttribute("trademarkList",trademarkList);
        //3:平台属性集合
        model.addAttribute("attrsList",attrsList);
        //4:商品集合
        model.addAttribute("goodsList",goodsList);
        //5:分页
        model.addAttribute("totalPages",totalPages);
        model.addAttribute("pageNo",vo.getPageNo());
        //6:urlParam
        String urlParam = buildUrlParam(searchParam);
        model.addAttribute("urlParam",urlParam);
        //7:排序    综合 （升|降）  底色（红底|白底）
        Map orderMap = buildOrderMap(searchParam);
        model.addAttribute("orderMap",orderMap);
        //8:条件回显
        //8.1:回显品牌   //trademark=2:华为
        String trademarkParam = buildTrademarkParam(searchParam);
        model.addAttribute("trademarkParam",trademarkParam);
        //8.2:回显平台属性及值
        List<Map> propsParamList = buildPropsParamList(searchParam);
        model.addAttribute("propsParamList",propsParamList);
        return "list/index";
    }

    //回显平台属性及值
    private List<Map> buildPropsParamList(SearchParam searchParam) {
        //  //props=23:4G:运行内存    ID:平台属性值:平台属性名称
        String[] props = searchParam.getProps();
        if(null != props && props.length > 0){
            return Arrays.stream(props).map(prop -> {
                Map map = new HashMap();
                String[] p = prop.split(":");
                map.put("attrId",p[0]);
                map.put("attrName",p[2]);
                map.put("attrValue",p[1]);
                return map;
            }).collect(Collectors.toList());
        }
        return null;
    }

    //回显品牌   //trademark=2:华为
    private String buildTrademarkParam(SearchParam searchParam) {
        String trademark = searchParam.getTrademark();
        if(!StringUtils.isEmpty(trademark)){
            String[] t = trademark.split(":");
            return "品牌:" + t[1];
        }
        return null;
    }

    //排序
    private Map buildOrderMap(SearchParam searchParam) {
        Map orderMap = new HashMap();

        // order=1:asc 1:desc  2:asc 2:desc
        String order = searchParam.getOrder();
        if(!StringUtils.isEmpty(order)){
            String[] o = order.split(":");
            orderMap.put("type",o[0]);
            orderMap.put("sort",o[1]);
        }else{
            orderMap.put("type","1");
            orderMap.put("sort","desc");
        }
        return orderMap;
    }

    //当前基础路径的拼接
    private String buildUrlParam(SearchParam searchParam) {

        StringBuilder sb = new StringBuilder();
        //StringBuffer  stringBuffer = new StringBuffer();
        //  http://list.gmall.com/list.html
        sb.append("http://list.gmall.com/list.html?");
        //关键词
        String keyword = searchParam.getKeyword();
        if(!StringUtils.isEmpty(keyword)){
            sb.append("keyword=").append(keyword);
        }
        //过滤条件
        //品牌
        String trademark = searchParam.getTrademark();
        if(!StringUtils.isEmpty(trademark)){
            sb.append("&trademark=").append(trademark);
        }
        //平台属性
        String[] props = searchParam.getProps();
        if(null != props && props.length > 0){
            for (String prop : props) {
                sb.append("&props=").append(prop);
            }
        }
        return sb.toString();
    }

}
